package com.zthz.douxing.sendmessageservice.handlers

import com.zthz.douxing.PlanTripSaveType
import com.zthz.douxing.SegmentStatus
import com.zthz.douxing.event.UserWillDepartEvent
import com.zthz.douxing.event.UserWillTripJob
import com.zthz.douxing.msg.MessageType
import com.zthz.douxing.sendmessageservice.thirdmessagesender.BaseMessageSenderService
import com.zthz.douxing.sendmessageservice.utils.OrderXingeMessageService
import com.zthz.douxing.util.CommonUtilService
import com.zthz.douxing.utils.CommonUtil
import grails.transaction.Transactional
import org.quartz.JobDataMap
import org.quartz.JobKey
import org.quartz.SimpleTrigger
import org.quartz.impl.triggers.SimpleTriggerImpl

import static org.quartz.JobBuilder.newJob

@Transactional
class PlanTripMessageArriveStatusHanlderService extends AbstractMessageHandlerService{

    @Override
    protected void handle(JobDataMap jobDataMap) {
        try {
            UserWillDepartEvent event =  jobDataMap.get("userWillTripEvent")
            if(!event?.getuiId && !event?.xgToken){
                return
            }

            if(event.tripSegment.startTime.getTime() - new Date().getTime()>0){
                Date surplusTime =  event.tripSegment.arriveTime

                def arriveStatusHandlerClosure = { JobDataMap jobmap->
                        try{
                            BaseMessageSenderService baseMessageSenderService = jobmap.get("baseMessageSenderService")
                            OrderXingeMessageService orderXingeMessageService = jobmap.get("orderXingeMessageService")

                            UserWillDepartEvent userWillTripEvent = jobmap.get("userWillTripEvent")
                            CommonUtilService commonUtilService = jobmap.get("commonUtilService")
                            String msgContent = orderXingeMessageService.orderMesageArriveStatus(userWillTripEvent)

                            if(msgContent){//发送消息
                                Map param = baseMessageSenderService
                                        .getParamFromUserWillDepartEvent4PlanForm(userWillTripEvent,msgContent,
                                        MessageType.PlanInfo)
                                String resultDesc = baseMessageSenderService.send2One(param)
                                orderXingeMessageService.saveMessageToUserLog(userWillTripEvent,
                                        msgContent,resultDesc,MessageType.PlanInfo)
                            }

                            String resultDesc = "更改状态成功"
                            String statusMsgContent = "更改行程的状态为在Arrived"
                            event.tripSegment.segmentStatus = SegmentStatus.Arrived

                            if(!commonUtilService.updateTripSegmentStauts(event.tripSegment))
                                resultDesc = "更改状态失败"
                            //更改状态
                            orderXingeMessageService.
                                    saveMessageToUserLog(userWillTripEvent,statusMsgContent,
                                            resultDesc,MessageType.Arrieded)
                        }catch (Exception e){
                            log.debug(e.getMessage(),e.printStackTrace())
                        }finally{
                        }

                }
                jobDataMap.put(JOBCONTENT_KEY,arriveStatusHandlerClosure)
                if(event?.tripSegment?.plantrip?.personOwner?.id){
                    String arriveStautsJob = CommonUtil.getJobKey("arrivedMessge",
                                                        event.tripSegment.plantrip.personOwner.id)
                    String arriveStautsTriggr = CommonUtil.getJobKey("arrivedTrigger",
                                                        event.tripSegment.plantrip.personOwner.id)
                    def jobKey = new JobKey(arriveStautsJob,"UserWillTrip")
                    def job = newJob(UserWillTripJob).withIdentity(jobKey).usingJobData(jobDataMap).build()
                    SimpleTrigger trigger =
                            new SimpleTriggerImpl(arriveStautsTriggr,
                                    surplusTime, null, 0, 0L)
                    quartzScheduler.deleteJob(jobKey)
                    addJob(job,trigger)
                }
            }
        } catch (Exception e) {
            throw e
        }
    }
}